/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GauntletDialog.java
 *
 * @author Josh Waters and Josh Dixon
 *
 * Created on Feb 17, 2011, 9:48:26 AM
 */
package org.workplicity.marist.grounds.ui;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.workplicity.marist.grounds.util.Report;
import org.workplicity.marist.grounds.util.Controller;
import org.workplicity.marist.print.ReportPrint;

public class GauntletDialog extends javax.swing.JDialog {

    /** Creates new form UI
     * @param parent
     * @param modal ensures user can not interact with Grounds Frame until
     * gauntlet dialog is closed
     */
    public GauntletDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Date endDate = new Date();
        Date startDate = new Date(endDate.getTime() - ONE_DAY);

        String startDateString = dateFormat.format(startDate);
        String endDateString = dateFormat.format(endDate);
        //Initialize fields
        SaveCheckBox.setEnabled(false);
        StartDateField.setText(startDateString);
        StartCalButton.setTargetDate(startDate);
        EndDateField.setText(endDateString);
        EndCalButton.setTargetDate(endDate);
        invalidCreateBox.setSelected(true);
        invalidServiceBox.setSelected(true);
        duplicateServiceBox.setSelected(true);
        purgeOldClosedReq.setSelected(true);
        purgeOldAndExpiredFutures.setSelected(true);
        serviceWithoutUser.setSelected(true);
        userWithSameLognames.setSelected(true);
        invalidLogname.setSelected(true);
        TabbedActionPanel.setSelectedIndex(0);
        Report.init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ReportDialog = new javax.swing.JDialog();
        ReportScrollPane = new javax.swing.JScrollPane();
        ReportEditorPane = new javax.swing.JEditorPane();
        ReportLabel = new javax.swing.JLabel();
        ReportCloseButton = new javax.swing.JButton();
        ReportPrintButton = new javax.swing.JButton();
        StartLabel = new javax.swing.JLabel();
        RepositoryLabel = new javax.swing.JLabel();
        TabbedActionPanel = new javax.swing.JTabbedPane();
        RepairPanel = new javax.swing.JPanel();
        invalidCreateBox = new javax.swing.JCheckBox();
        invalidUpdateBox = new javax.swing.JCheckBox();
        invalidUserBox = new javax.swing.JCheckBox();
        invalidWorkSlateBox = new javax.swing.JCheckBox();
        invalidServiceBox = new javax.swing.JCheckBox();
        duplicateWorkSlateBox = new javax.swing.JCheckBox();
        duplicateServiceBox = new javax.swing.JCheckBox();
        PurgePanel = new javax.swing.JPanel();
        purgeReqNotInOpenClosedAndNotInWs = new javax.swing.JCheckBox();
        purgeOldClosedReq = new javax.swing.JCheckBox();
        purgeFutureWithInvalidTemplate = new javax.swing.JCheckBox();
        purgeOldAndExpiredFutures = new javax.swing.JCheckBox();
        OldIfPurgeLabel = new javax.swing.JLabel();
        OldIfComboBox = new javax.swing.JComboBox();
        WarnPanel = new javax.swing.JPanel();
        serviceWithoutUser = new javax.swing.JCheckBox();
        userWithoutService = new javax.swing.JCheckBox();
        futureInvalidTemplate = new javax.swing.JCheckBox();
        expiredFutures = new javax.swing.JCheckBox();
        userWithSameLognames = new javax.swing.JCheckBox();
        duplicateIds = new javax.swing.JCheckBox();
        requestInvalidService = new javax.swing.JCheckBox();
        invalidLogname = new javax.swing.JCheckBox();
        invalidPassword = new javax.swing.JCheckBox();
        ByeButton = new javax.swing.JButton();
        RepoComboBox = new javax.swing.JComboBox();
        EndLabel = new javax.swing.JLabel();
        StartDateField = new javax.swing.JTextField();
        EndDateField = new javax.swing.JTextField();
        GoButton = new javax.swing.JButton();
        SaveCheckBox = new javax.swing.JCheckBox();
        StartCalButton = new net.sourceforge.jcalendarbutton.JCalendarButton();
        EndCalButton = new net.sourceforge.jcalendarbutton.JCalendarButton();
        ReportButton = new javax.swing.JButton();

        ReportDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        ReportDialog.setTitle("Gauntlet Report");

        ReportEditorPane.setFont(new java.awt.Font("Courier New", 0, 11));
        ReportScrollPane.setViewportView(ReportEditorPane);

        ReportLabel.setText("Report:");

        ReportCloseButton.setText("Close");
        ReportCloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReportCloseButtonActionPerformed(evt);
            }
        });

        ReportPrintButton.setText("Print");
        ReportPrintButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReportPrintButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ReportDialogLayout = new javax.swing.GroupLayout(ReportDialog.getContentPane());
        ReportDialog.getContentPane().setLayout(ReportDialogLayout);
        ReportDialogLayout.setHorizontalGroup(
            ReportDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ReportDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ReportDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ReportScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
                    .addComponent(ReportLabel)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ReportDialogLayout.createSequentialGroup()
                        .addComponent(ReportPrintButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ReportCloseButton)))
                .addContainerGap())
        );
        ReportDialogLayout.setVerticalGroup(
            ReportDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ReportDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ReportLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ReportScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(ReportDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ReportCloseButton)
                    .addComponent(ReportPrintButton))
                .addGap(14, 14, 14))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Repository Maintenance");

        StartLabel.setText("Start:");

        RepositoryLabel.setText("Repository:");

        TabbedActionPanel.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                TabbedActionPanelStateChanged(evt);
            }
        });

        RepairPanel.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                RepairPanelPropertyChange(evt);
            }
        });

        invalidCreateBox.setText("Fix invalid create date.");

        invalidUpdateBox.setText("Fix invalid update date.");

        invalidUserBox.setText("Fix invalid update user.");

        invalidWorkSlateBox.setText("Fix invalid work slate references.");

        invalidServiceBox.setText("Fix invalid or missing service references.");

        duplicateWorkSlateBox.setText("Fix duplicate work slate references.");

        duplicateServiceBox.setText("Fix duplicate service references.");

        javax.swing.GroupLayout RepairPanelLayout = new javax.swing.GroupLayout(RepairPanel);
        RepairPanel.setLayout(RepairPanelLayout);
        RepairPanelLayout.setHorizontalGroup(
            RepairPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RepairPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(RepairPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(invalidCreateBox)
                    .addComponent(invalidUpdateBox)
                    .addComponent(invalidUserBox)
                    .addComponent(invalidWorkSlateBox)
                    .addComponent(invalidServiceBox)
                    .addComponent(duplicateWorkSlateBox)
                    .addComponent(duplicateServiceBox))
                .addContainerGap(121, Short.MAX_VALUE))
        );
        RepairPanelLayout.setVerticalGroup(
            RepairPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RepairPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(invalidCreateBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(invalidUpdateBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(invalidUserBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(invalidWorkSlateBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(invalidServiceBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(duplicateWorkSlateBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(duplicateServiceBox)
                .addContainerGap(51, Short.MAX_VALUE))
        );

        TabbedActionPanel.addTab("Repair", RepairPanel);

        PurgePanel.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                PurgePanelPropertyChange(evt);
            }
        });

        purgeReqNotInOpenClosedAndNotInWs.setText("Purge requests not in OPEN/CLOSED and not on w/s.");

        purgeOldClosedReq.setText("Purge old CLOSED requests.");

        purgeFutureWithInvalidTemplate.setText("Purge repositories of entries not in template class.");

        purgeOldAndExpiredFutures.setText("Purge old disabled and expired future events.");

        OldIfPurgeLabel.setText("Old if:");

        OldIfComboBox.setMaximumRowCount(5);
        OldIfComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No Selection", "> 1 Day", "> 1 Week", "> 1 Month", "> 1 Year" }));
        OldIfComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OldIfComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PurgePanelLayout = new javax.swing.GroupLayout(PurgePanel);
        PurgePanel.setLayout(PurgePanelLayout);
        PurgePanelLayout.setHorizontalGroup(
            PurgePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PurgePanelLayout.createSequentialGroup()
                .addGroup(PurgePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PurgePanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(PurgePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(purgeOldClosedReq)
                            .addComponent(purgeFutureWithInvalidTemplate)
                            .addComponent(purgeReqNotInOpenClosedAndNotInWs)
                            .addComponent(purgeOldAndExpiredFutures)))
                    .addGroup(PurgePanelLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(OldIfPurgeLabel)
                        .addGap(18, 18, 18)
                        .addComponent(OldIfComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(59, Short.MAX_VALUE))
        );
        PurgePanelLayout.setVerticalGroup(
            PurgePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PurgePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(purgeReqNotInOpenClosedAndNotInWs)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(purgeOldClosedReq)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(purgeFutureWithInvalidTemplate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(purgeOldAndExpiredFutures)
                .addGap(42, 42, 42)
                .addGroup(PurgePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OldIfPurgeLabel)
                    .addComponent(OldIfComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(58, Short.MAX_VALUE))
        );

        TabbedActionPanel.addTab("Purge", PurgePanel);

        WarnPanel.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                WarnPanelPropertyChange(evt);
            }
        });

        serviceWithoutUser.setText("Services without users.");

        userWithoutService.setText("Users without services.");

        futureInvalidTemplate.setText("Futures with invalid templates.");

        expiredFutures.setText("Enable and expired futures.");

        userWithSameLognames.setText("Users with identical log names.");

        duplicateIds.setText("Duplicate ids.");

        requestInvalidService.setText("Requests with invalid or missing service references.");

        invalidLogname.setText("Invalid or missing log name.");

        invalidPassword.setText("Invalid or insecure password.");

        javax.swing.GroupLayout WarnPanelLayout = new javax.swing.GroupLayout(WarnPanel);
        WarnPanel.setLayout(WarnPanelLayout);
        WarnPanelLayout.setHorizontalGroup(
            WarnPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(WarnPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(WarnPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(serviceWithoutUser)
                    .addComponent(userWithoutService)
                    .addComponent(futureInvalidTemplate)
                    .addComponent(expiredFutures)
                    .addComponent(userWithSameLognames)
                    .addComponent(duplicateIds)
                    .addComponent(requestInvalidService)
                    .addComponent(invalidLogname)
                    .addComponent(invalidPassword))
                .addContainerGap(67, Short.MAX_VALUE))
        );
        WarnPanelLayout.setVerticalGroup(
            WarnPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(WarnPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(serviceWithoutUser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(userWithoutService)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(futureInvalidTemplate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(expiredFutures)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(userWithSameLognames)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(duplicateIds)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(requestInvalidService)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(invalidLogname)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(invalidPassword)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        TabbedActionPanel.addTab("Warn", WarnPanel);

        ByeButton.setText("Bye");
        ByeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ByeButtonActionPerformed(evt);
            }
        });

        RepoComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ALL", "Accounts", "Clearinghouse", "WorkSlates", "Services", "Futures" }));
        RepoComboBox.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                RepoComboBoxPropertyChange(evt);
            }
        });

        EndLabel.setText("End:");

        StartDateField.setText("00/00/00");
        StartDateField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                StartDateFieldFocusLost(evt);
            }
        });

        EndDateField.setText("00/00/00");
        EndDateField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                EndDateFieldFocusLost(evt);
            }
        });

        GoButton.setForeground(new java.awt.Color(0, 0, 204));
        GoButton.setText("Go");
        GoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GoButtonActionPerformed(evt);
            }
        });

        SaveCheckBox.setText("Save settings");

        StartCalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartCalButtonActionPerformed(evt);
            }
        });
        StartCalButton.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                StartCalButtonPropertyChange(evt);
            }
        });

        EndCalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EndCalButtonActionPerformed(evt);
            }
        });
        EndCalButton.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                EndCalButtonPropertyChange(evt);
            }
        });

        ReportButton.setText("Report");
        ReportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReportButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(SaveCheckBox)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                                .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ReportButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ByeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(8, 8, 8))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(EndLabel)
                                    .addComponent(StartLabel)
                                    .addComponent(RepositoryLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(RepoComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(EndDateField)
                                            .addComponent(StartDateField, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(EndCalButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(StartCalButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 182, Short.MAX_VALUE))))
                    .addComponent(TabbedActionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RepositoryLabel)
                    .addComponent(RepoComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StartLabel)
                    .addComponent(StartDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(StartCalButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(EndLabel)
                    .addComponent(EndDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(EndCalButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TabbedActionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SaveCheckBox)
                    .addComponent(ByeButton)
                    .addComponent(ReportButton)
                    .addComponent(GoButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ByeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ByeButtonActionPerformed
        // Closes the Gauntlet tools GUI application window.
        dispose();
}//GEN-LAST:event_ByeButtonActionPerformed

    private void StartDateFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_StartDateFieldFocusLost
        // Sets the StartDate text filed with the date recived from the
        // calander button or text entry.

        String date = StartDateField.getText();
        setStartDate(date);
    }//GEN-LAST:event_StartDateFieldFocusLost

    private void StartCalButtonPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_StartCalButtonPropertyChange
        // Passes the start-date that is clicked on inside of the calander popup to the textfield.

        if (evt.getNewValue() instanceof Date) {
            setStartDate((Date) evt.getNewValue());
        }
    }//GEN-LAST:event_StartCalButtonPropertyChange

    private void EndDateFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_EndDateFieldFocusLost
        // Sets the EndDate text filed with the date recived from the
        // calander button or text entry.

        String date = EndDateField.getText();
        setEndDate(date);
    }//GEN-LAST:event_EndDateFieldFocusLost

    private void EndCalButtonPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_EndCalButtonPropertyChange
        // Passes the end-date that is clicked on inside of the calander popup to the textfield.

        if (evt.getNewValue() instanceof Date) {
            setEndDate((Date) evt.getNewValue());
        }
    }//GEN-LAST:event_EndCalButtonPropertyChange
    final long infiniteEndDate = Long.MAX_VALUE;
    private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GoButtonActionPerformed

        ReportDialog.setBounds(50, 50, 650, 350);
        ReportEditorPane.setEditable(false);
        // If no boxs are selected, a dialog box will inform ther user, but if
        // a box is selected, than the corresponding method will be executed.
        if (TabbedActionPanel.getSelectedIndex() == 0) {
            // ***************************
            // Repair Tab Checkbox Checker
            // ***************************
            Report.resetNumRepaired();
            if (invalidCreateBox.isSelected()
                    || invalidUpdateBox.isSelected()
                    || invalidUserBox.isSelected()
                    || invalidWorkSlateBox.isSelected()
                    || invalidServiceBox.isSelected()
                    || duplicateWorkSlateBox.isSelected()
                    || duplicateServiceBox.isSelected() == true) {
                //Controller.loadTestRepos();
                Controller.updateRepos();
                boolean[] repairCheckBox = new boolean[7];
                repairCheckBox[0] = invalidCreateBox.isSelected();
                repairCheckBox[1] = invalidUpdateBox.isSelected();
                repairCheckBox[2] = invalidUserBox.isSelected();
                repairCheckBox[3] = invalidWorkSlateBox.isSelected();
                repairCheckBox[4] = invalidServiceBox.isSelected();
                repairCheckBox[5] = duplicateWorkSlateBox.isSelected();
                repairCheckBox[6] = duplicateServiceBox.isSelected();

                try {
                    long startDate = dateFormat.parse(StartDateField.getText()).getTime();
                    long endDate = dateFormat.parse(EndDateField.getText()).getTime();

                    Controller.repair(repairCheckBox, startDate, endDate);
                } catch (Exception e) {
                    System.out.println(e);
                }
//              JOptionPane.showMessageDialog(null, Report.numRepaired + " repair(s) have been performed and were added to the report", "Actions taken", JOptionPane.WARNING_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "No Check boxes have been selected!", "Oops!", JOptionPane.WARNING_MESSAGE);
            }
        } else if (TabbedActionPanel.getSelectedIndex() == 1) {
            // ***************************
            // Purge Tab Checkbox Checker:
            // ***************************
            Report.resetNumPurged();
            if (purgeReqNotInOpenClosedAndNotInWs.isSelected()
                    || purgeOldClosedReq.isSelected()
                    || purgeFutureWithInvalidTemplate.isSelected()
                    || purgeOldAndExpiredFutures.isSelected() == true) {
                int n = JOptionPane.showConfirmDialog(null, "You are about to purge the repository, continue?", "Warning", JOptionPane.YES_NO_OPTION);
                if (n == 1) {
                    return;
                }
                //controller.loadTestRepos();
                //Controller.updateRepos();
                boolean[] purgeCheckBox = new boolean[4];
                purgeCheckBox[0] = purgeReqNotInOpenClosedAndNotInWs.isSelected();
                purgeCheckBox[1] = purgeOldClosedReq.isSelected();
                purgeCheckBox[2] = purgeFutureWithInvalidTemplate.isSelected();
                purgeCheckBox[3] = purgeOldAndExpiredFutures.isSelected();

                //If the older if combo box is anything but NO SELECTION, get the OlderThan
                //variable and call purge with infiniteEndDate
                if (OldIfComboBox.getSelectedIndex() != 0) {
                    Controller.purge(purgeCheckBox, getOlderThan(), infiniteEndDate);
                } //No older than selection was made, use the date fields
                else {
                    try {
                        long startDate = dateFormat.parse(StartDateField.getText()).getTime();
                        long endDate = dateFormat.parse(EndDateField.getText()).getTime();
                        // SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
                        Controller.purge(purgeCheckBox, startDate, endDate);
                    } catch (Exception e) {
                    }
                }
            } //  Controller.purgeRepositories();
            //              JOptionPane.showMessageDialog(null, Report.numPurged + " purge(s) have been performed and were added to the report.", "Actions taken", JOptionPane.WARNING_MESSAGE);
            else {
                JOptionPane.showMessageDialog(null, "No Check boxes have been selected!", "Oops!", JOptionPane.WARNING_MESSAGE);
            }
        } else if (TabbedActionPanel.getSelectedIndex() == 2) {
            // ***************************
            // Warning Tab Checkbox Checker
            // ***************************
            Report.resetWarnings();
            if (serviceWithoutUser.isSelected()
                    || userWithoutService.isSelected()
                    || futureInvalidTemplate.isSelected()
                    || expiredFutures.isSelected()
                    || userWithSameLognames.isSelected()
                    || duplicateIds.isSelected()
                    || requestInvalidService.isSelected()
                    || invalidLogname.isSelected()
                    || invalidPassword.isSelected() == true) {
                //Controller.loadTestRepos();
                Controller.updateRepos();
                boolean[] checkBox = new boolean[9];

                checkBox[0] = serviceWithoutUser.isSelected();
                checkBox[1] = userWithoutService.isSelected();
                checkBox[2] = futureInvalidTemplate.isSelected();
                checkBox[3] = expiredFutures.isSelected();
                checkBox[4] = userWithSameLognames.isSelected();
                checkBox[5] = duplicateIds.isSelected();
                checkBox[6] = requestInvalidService.isSelected();
                checkBox[7] = invalidLogname.isSelected();
                checkBox[8] = invalidPassword.isSelected();
                Controller.warn(checkBox);
                //JOptionPane.showMessageDialog(null, Report.numWarnings + " warning(s) have been added to the report.", "Actions taken", JOptionPane.WARNING_MESSAGE);

            } else {
                JOptionPane.showMessageDialog(null, "No Check boxes have been selected!", "Oops!", JOptionPane.WARNING_MESSAGE);
            }
        }
        repaint();
    }//GEN-LAST:event_GoButtonActionPerformed

    private void RepairPanelPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_RepairPanelPropertyChange
        // When the Repair tab is selected, it sets the index in order to be referenced at a later time.

        TabbedActionPanel.setSelectedIndex(0);
    }//GEN-LAST:event_RepairPanelPropertyChange

    private void PurgePanelPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_PurgePanelPropertyChange
        // When the Purge tab is selected, it sets the index in order to be referenced at a later time.

        TabbedActionPanel.setSelectedIndex(1);
    }//GEN-LAST:event_PurgePanelPropertyChange

    private void WarnPanelPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_WarnPanelPropertyChange
        // When the Warning tab is selected, it sets the index in order to be referenced at a later time.

        TabbedActionPanel.setSelectedIndex(2);
    }//GEN-LAST:event_WarnPanelPropertyChange

    private void TabbedActionPanelStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_TabbedActionPanelStateChanged
        // When the Purge tab is selected,  the RepoComboBox is set to the 'ALL'
        // repository, and the box is then disabled so as not to delete from the 
        // wrong repo.  When either of the other tabs are selected, it is re-enabled.

        if (TabbedActionPanel.getSelectedIndex() == 1 || TabbedActionPanel.getSelectedIndex() == 2) {
            RepoComboBox.setSelectedIndex(0);
            RepoComboBox.setEnabled(false);
        } else {
            RepoComboBox.setEnabled(true);
        }
    }//GEN-LAST:event_TabbedActionPanelStateChanged

    private void RepoComboBoxPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_RepoComboBoxPropertyChange

        int oldIf = OldIfComboBox.getSelectedIndex();

    }//GEN-LAST:event_RepoComboBoxPropertyChange

    private void ReportCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReportCloseButtonActionPerformed

        ReportDialog.setModal(false);
        ReportDialog.setVisible(false);


    }//GEN-LAST:event_ReportCloseButtonActionPerformed

    private void ReportPrintButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReportPrintButtonActionPerformed
        ReportPrint reportPrint = new ReportPrint();
        reportPrint.printReport();
    }//GEN-LAST:event_ReportPrintButtonActionPerformed

    private void OldIfComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OldIfComboBoxActionPerformed
        if (OldIfComboBox.getSelectedIndex() != 0) {
            StartDateField.setEnabled(false);
            EndDateField.setEnabled(false);
        } else {

            StartDateField.setEnabled(true);
            EndDateField.setEnabled(true);
        }
    }//GEN-LAST:event_OldIfComboBoxActionPerformed

    private void StartCalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartCalButtonActionPerformed
        StartDateField.setEnabled(true);
        EndDateField.setEnabled(true);
        // OldIfComboBox.setSelectedIndex(0); **need to modify OlderThan method to account for blank
    }//GEN-LAST:event_StartCalButtonActionPerformed

    private void EndCalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EndCalButtonActionPerformed
        StartDateField.setEnabled(true);
        EndDateField.setEnabled(true);
        // OldIfComboBox.setSelectedIndex(0); **need to modify OlderThan method to account for blank
    }//GEN-LAST:event_EndCalButtonActionPerformed

    private void ReportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReportButtonActionPerformed
        ReportEditorPane.setText(Report.output());
        ReportDialog.setModal(true);
        ReportDialog.setVisible(true);
    }//GEN-LAST:event_ReportButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(GauntletDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(GauntletDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(GauntletDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(GauntletDialog.class.getName()).log(Level.SEVERE, null, ex);
                }

                GauntletDialog dialog = new GauntletDialog(new javax.swing.JFrame(), true);

                System.out.println("Report Initialized");
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ByeButton;
    private net.sourceforge.jcalendarbutton.JCalendarButton EndCalButton;
    private javax.swing.JTextField EndDateField;
    private javax.swing.JLabel EndLabel;
    private javax.swing.JButton GoButton;
    private javax.swing.JComboBox OldIfComboBox;
    private javax.swing.JLabel OldIfPurgeLabel;
    private javax.swing.JPanel PurgePanel;
    private javax.swing.JPanel RepairPanel;
    private javax.swing.JComboBox RepoComboBox;
    private javax.swing.JButton ReportButton;
    private javax.swing.JButton ReportCloseButton;
    private javax.swing.JDialog ReportDialog;
    private javax.swing.JEditorPane ReportEditorPane;
    private javax.swing.JLabel ReportLabel;
    private javax.swing.JButton ReportPrintButton;
    private javax.swing.JScrollPane ReportScrollPane;
    private javax.swing.JLabel RepositoryLabel;
    private javax.swing.JCheckBox SaveCheckBox;
    private net.sourceforge.jcalendarbutton.JCalendarButton StartCalButton;
    private javax.swing.JTextField StartDateField;
    private javax.swing.JLabel StartLabel;
    private javax.swing.JTabbedPane TabbedActionPanel;
    private javax.swing.JPanel WarnPanel;
    private javax.swing.JCheckBox duplicateIds;
    private javax.swing.JCheckBox duplicateServiceBox;
    private javax.swing.JCheckBox duplicateWorkSlateBox;
    private javax.swing.JCheckBox expiredFutures;
    private javax.swing.JCheckBox futureInvalidTemplate;
    private javax.swing.JCheckBox invalidCreateBox;
    private javax.swing.JCheckBox invalidLogname;
    private javax.swing.JCheckBox invalidPassword;
    private javax.swing.JCheckBox invalidServiceBox;
    private javax.swing.JCheckBox invalidUpdateBox;
    private javax.swing.JCheckBox invalidUserBox;
    private javax.swing.JCheckBox invalidWorkSlateBox;
    private javax.swing.JCheckBox purgeFutureWithInvalidTemplate;
    private javax.swing.JCheckBox purgeOldAndExpiredFutures;
    private javax.swing.JCheckBox purgeOldClosedReq;
    private javax.swing.JCheckBox purgeReqNotInOpenClosedAndNotInWs;
    private javax.swing.JCheckBox requestInvalidService;
    private javax.swing.JCheckBox serviceWithoutUser;
    private javax.swing.JCheckBox userWithSameLognames;
    private javax.swing.JCheckBox userWithoutService;
    // End of variables declaration//GEN-END:variables
    /**
     *
     */
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

    /**
     * Validate and set the datetime field on the screen given a datetime string.
     * @param dateString
     */
    public void setStartDate(String dateString) {
        Date date = null;
        try {
            if ((dateString != null) && (dateString.length() > 0)) {
                date = dateFormat.parse(dateString);
            }
        } catch (Exception e) {
            date = null;
        }
        this.setStartDate(date);
    }

    /**
     * Validate and set the datetime field on the screen given a date.
     * @param date
     */
    public void setStartDate(Date date) {
        String dateString = "";
        if (date != null) {
            dateString = dateFormat.format(date);
        }
        StartDateField.setText(dateString);
        StartCalButton.setTargetDate(date);
    }

    /**
     * Validate and set the datetime field on the screen given a datetime string.
     * @param dateString
     */
    public void setEndDate(String dateString) {
        Date date = null;
        try {
            if ((dateString != null) && (dateString.length() > 0)) {
                date = dateFormat.parse(dateString);
            }
        } catch (Exception e) {
            date = null;
        }
        this.setEndDate(date);
    }

    /**
     * Validate and set the datetime field on the screen given a date.
     * @param date
     */
    public void setEndDate(Date date) {
        String dateString = "";
        if (date != null) {
            dateString = dateFormat.format(date);
        }
        EndDateField.setText(dateString);
        EndCalButton.setTargetDate(date);
    }

    /**
     * Used for Testing
     */
    public void purgeNotInOpenClosedOrWs() {
        System.out.println("The box[0] was checked!!");
    }

    /**
     * Used for Testing
     */
    public void purgeOldClosedReq() {
        System.out.println("The box[1] was checked!!");
    }

    /**
     * Used for Testing
     */
    public void purgeRepOfEntryNotInTemp() {
        System.out.println("The box[2] was checked!!");
    }

    /**
     * Used for Testing
     */
    public void purgeOldAndExpiredFutures() {
        System.out.println("The box[3] was checked!!");
    }
    /*
     * This method calculates the time in milliseconds based on the user
     * selection used to determine if an item is old
     * @return a variable of type long that is the representation of 1 day,
     * 1 week, 1 month, or 1 year in milliseconds
     */
    final long ONE_DAY = 1000 * 60 * 60 * 24;

    private long getOlderThan() {
        int oldIf = OldIfComboBox.getSelectedIndex();
        long olderThan;
        switch (oldIf) {
            case 0:
                olderThan = 0;
                break;
            case 1:
                olderThan = ONE_DAY;
                break;
            case 2:
                olderThan = ONE_DAY * 7;
                break;
            case 3:
                olderThan = ONE_DAY * 30;
                break;
            case 4:
                olderThan = ONE_DAY * 365;
                break;
            default:
                olderThan = 0;
                break;
        }

        return olderThan;
    }
}
